﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Devv.Core.Utils;
using Yglu.BusinessLayer;
using Yglu.BusinessLayer.Entities;

namespace Yglu.TerminalApp
{
	static class Program
	{
		#region Properties

		public static MainForm MainForm { get; set; }

		public static SystemConfig SystemConfig { get; set; }
		public static Terminal LoggedTerminal { get; set; }
		public static User LoggedUser { get; set; }
		public static PromotionCollection Promotions { get; set; }
		public static ImageList ProductIcons { get; set; }

		#endregion

		#region Main methods

		[STAThread]
		public static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			MainForm = new MainForm();

			LoadConfig();
			LoadPromotions();
			LoadProductIcons();

			Application.Run(MainForm);
		}

		public static void LoadConfig()
		{
			Devv.Core.Erroo.Config.LoadFromXml();
			Devv.Core.UniDataTier.Config.LoadFromXml("Yglu");

			SystemConfig = new SystemConfig(DataAccess.SystemConfig_Get());
            
			LoggedTerminal = new Terminal(ConfigUtil.GetConfig("Yglu.Terminal"));

			if (LoggedTerminal.Id < 1 || !LoggedTerminal.Active)
			{
				MessageBox.Show(Messages.TerminalInvalid);
				Application.Exit();
			}
		}

		public static void LoadPromotions()
		{
			Promotions = new PromotionCollection(DataAccess.Promotion_Get());
		}

		public static void LoadProductIcons()
		{
			ProductIcons = new ImageList();

			ProductIcons.ColorDepth = ColorDepth.Depth32Bit;
			ProductIcons.ImageSize = new Size(48, 48);

			foreach (String file in Directory.GetFiles(SystemConfig.ProductIconsPath))
			{
				ProductIcons.Images.Add(Path.GetFileNameWithoutExtension(file), new Bitmap(file));
			}
		}

		#endregion

		#region Helpers

		public static void PopError(String text)
		{
			using (PopupForm pop = new PopupForm(true, text))
			{
				pop.ShowDialog();
				pop.Focus();
			}
		}

		public static void PopConfirmation(String text)
		{
			using (PopupForm pop = new PopupForm(false, text))
			{
				pop.ShowDialog();
				pop.Focus();
			}
		}

		public static Boolean ConfirmAction(String text)
		{
			using (ConfirmActionForm f = new ConfirmActionForm(text))
			{
				f.ShowDialog();

				return (f.DialogResult == DialogResult.Yes);
			}
		}

		#endregion

		#region InputValidation

		public static Boolean InputIsNumber(Keys k)
		{
			Boolean result = (k == Keys.D0 ||
							k == Keys.D1 ||
							k == Keys.D2 ||
							k == Keys.D3 ||
							k == Keys.D4 ||
							k == Keys.D5 ||
							k == Keys.D6 ||
							k == Keys.D7 ||
							k == Keys.D8 ||
							k == Keys.D9 ||
							k == Keys.NumPad0 ||
							k == Keys.NumPad1 ||
							k == Keys.NumPad2 ||
							k == Keys.NumPad3 ||
							k == Keys.NumPad4 ||
							k == Keys.NumPad5 ||
							k == Keys.NumPad6 ||
							k == Keys.NumPad7 ||
							k == Keys.NumPad8 ||
							k == Keys.NumPad9 ||
							k == Keys.Oemcomma ||
							k == Keys.OemPeriod ||
							k == Keys.OemMinus ||
							k == Keys.Back ||
							k == Keys.Delete ||
							k == Keys.Enter
						   );

			return result;
		}

		#endregion

		#region Errors

		public static void LogError(Exception ex, String message)
		{
			Devv.Core.Erroo.ErrorHandler.Handle(ex, message);
		}

		#endregion
	}
}
